package com.coderman.common.starter.tianqing.qlexpress.service;

import com.ql.util.express.IExpressContext;
import org.springframework.context.ApplicationContext;

import java.util.HashMap;
import java.util.Map;

/**
 * Description:
 * date: 2020/10/14
 *
 * @author fanchunshuai
 * @version 1.0.0
 * @since JDK 1.8
 */
public class SpringQlExpressBeanContext extends HashMap<String,Object> implements IExpressContext<String,Object> {
    private ApplicationContext applicationContext;
    public SpringQlExpressBeanContext(Map<String,Object> aProperties, ApplicationContext applicationContext)
    {
        super(aProperties);
        this.applicationContext = applicationContext;
    }
    /** * 根据key从容器里面获取对象 * * @param key * @return */
    @Override
    public Object get(Object key) {
        Object object = super.get(key);
        try {
            if (object == null && this.applicationContext != null
                    && this.applicationContext.containsBean((String)key))
            {
                object = this.applicationContext.getBean((String)key);
            }
        } catch (Exception e) {
            throw new RuntimeException("表达式容器获取对象失败", e);
        }
        return object;
    }
    /** * 把key-value放到容器里面去 * * @param key * @param value */
    @Override
    public Object put(String key, Object value) {
        return super.put(key, value);
    }
}
